package com.example.main;

import java.util.Random;
import java.util.Vector;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

public class PlayCards extends View{

	
	Coint gCoint;
	
	
	Bitmap bgr;
	boolean touch;
	float eventX;
	float eventY;
	int x = 100;
	
	boolean bDrawLost = false;
	
	int nNextHit = 2;
	
	Bitmap bBtnDeal;
	Bitmap bBtnHit;
	Bitmap bBtnStand;
	Bitmap bBtnDouble;

	Random Generator = new Random();
	
	Bitmap back_card;
	
	enum State{
		eStateBegin,
		eStateDeal,
		eStateStand,
		
	};

	State eState;
	
	int width = 0;
	int height = 0;
	int bButtonDeal_H = 0;
	int bButtonDeal_W = 0;
	boolean bDealTrue = true;
	
	boolean bNotBet = false;
	
	int widthHit = 0;
	int heighHitt = 0;
	int bButtonHit_H = 0;
	int bButtonHit_W = 0;
	
	int bButtonStand_H = 0;
	int bButtonStand_W = 0;
	
	int bButtonDouble_H = 0;
	int bButtonDouble_W = 0;
	
	int bCard_H = 0;

	int Points = 0;
	int PointsCrupie = 0;
	
	Vector <Bitmap> v_PlayerCard = new Vector<Bitmap>(0);
	Vector <Bitmap> v_CropieCard = new Vector<Bitmap>(0);
	
	int v_PlayerPoints[] = new int[10];
	int v_CropiePoints[] = new int[10];

	int nFontSize = 0;
	
	int CardsBet [] = {
		11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
		11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
		11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
		11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,	
	};
	
	Bitmap[] bitmaps = new Bitmap[] {
			
			//spatia
			BitmapFactory.decodeResource(getResources(), R.drawable.as),  
			BitmapFactory.decodeResource(getResources(), R.drawable.twos),
			BitmapFactory.decodeResource(getResources(), R.drawable.thres),
			BitmapFactory.decodeResource(getResources(), R.drawable.fours),
			BitmapFactory.decodeResource(getResources(), R.drawable.fives),
			BitmapFactory.decodeResource(getResources(), R.drawable.sixs),
			BitmapFactory.decodeResource(getResources(), R.drawable.sevens),
			BitmapFactory.decodeResource(getResources(), R.drawable.eits),
			BitmapFactory.decodeResource(getResources(), R.drawable.nines),
			BitmapFactory.decodeResource(getResources(), R.drawable.tens),
			BitmapFactory.decodeResource(getResources(), R.drawable.js),
			BitmapFactory.decodeResource(getResources(), R.drawable.qs),
			BitmapFactory.decodeResource(getResources(), R.drawable.ps),
			
			//pica
			BitmapFactory.decodeResource(getResources(), R.drawable.ap),  
			BitmapFactory.decodeResource(getResources(), R.drawable.twop),
			BitmapFactory.decodeResource(getResources(), R.drawable.threp),
			BitmapFactory.decodeResource(getResources(), R.drawable.fourp),
			BitmapFactory.decodeResource(getResources(), R.drawable.fivep),
			BitmapFactory.decodeResource(getResources(), R.drawable.sixp),
			BitmapFactory.decodeResource(getResources(), R.drawable.sevenp),
			BitmapFactory.decodeResource(getResources(), R.drawable.eitp),
			BitmapFactory.decodeResource(getResources(), R.drawable.ninep),
			BitmapFactory.decodeResource(getResources(), R.drawable.tenp),
			BitmapFactory.decodeResource(getResources(), R.drawable.jp),
			BitmapFactory.decodeResource(getResources(), R.drawable.qp),
			BitmapFactory.decodeResource(getResources(), R.drawable.pp),
			
			//karo
			BitmapFactory.decodeResource(getResources(), R.drawable.ak),  
			BitmapFactory.decodeResource(getResources(), R.drawable.twok),
			BitmapFactory.decodeResource(getResources(), R.drawable.threk),
			BitmapFactory.decodeResource(getResources(), R.drawable.fourk),
			BitmapFactory.decodeResource(getResources(), R.drawable.fivek),
			BitmapFactory.decodeResource(getResources(), R.drawable.sixk),
			BitmapFactory.decodeResource(getResources(), R.drawable.sevenk),
			BitmapFactory.decodeResource(getResources(), R.drawable.eitk),
			BitmapFactory.decodeResource(getResources(), R.drawable.ninek),
			BitmapFactory.decodeResource(getResources(), R.drawable.tenk),
			BitmapFactory.decodeResource(getResources(), R.drawable.jk),
			BitmapFactory.decodeResource(getResources(), R.drawable.qk),
			BitmapFactory.decodeResource(getResources(), R.drawable.pk),
			
			//kupa
			BitmapFactory.decodeResource(getResources(), R.drawable.a1),  
			BitmapFactory.decodeResource(getResources(), R.drawable.two1),
			BitmapFactory.decodeResource(getResources(), R.drawable.thre1),
			BitmapFactory.decodeResource(getResources(), R.drawable.four1),
			BitmapFactory.decodeResource(getResources(), R.drawable.five1),
			BitmapFactory.decodeResource(getResources(), R.drawable.six1),
			BitmapFactory.decodeResource(getResources(), R.drawable.seven1),
			BitmapFactory.decodeResource(getResources(), R.drawable.eit1),
			BitmapFactory.decodeResource(getResources(), R.drawable.nine1),
			BitmapFactory.decodeResource(getResources(), R.drawable.ten1),
			BitmapFactory.decodeResource(getResources(), R.drawable.j1),
			BitmapFactory.decodeResource(getResources(), R.drawable.q1),
			BitmapFactory.decodeResource(getResources(), R.drawable.p1),
			
	
	};
	
	public PlayCards(Context context) {
		super(context);
		
		gCoint = new Coint(context);
		
		bCard_H = bitmaps[0].getHeight();

		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		width = metrics.widthPixels;
		height = metrics.heightPixels;
		
		if (height > 800){
			nFontSize = 40;
		}else if(height <= 800 && height > 480){
			nFontSize = 30;
		}else if (width == 320){
			nFontSize = 20;
		}
		
		bBtnDeal = BitmapFactory.decodeResource(getResources(), R.drawable.btn_deal);
		
		bButtonDeal_H = bBtnDeal.getHeight();
		bButtonDeal_W = bBtnDeal.getWidth();
		
		bBtnHit = BitmapFactory.decodeResource(getResources(), R.drawable.btn_hit);
		bButtonHit_H = bBtnHit.getHeight();
		bButtonHit_W = bBtnHit.getWidth();
		
		bBtnStand = BitmapFactory.decodeResource(getResources(), R.drawable.btn_stand);
		bButtonStand_H = bBtnStand.getHeight();
		bButtonStand_W = bBtnStand.getWidth();
	
		bBtnDouble = BitmapFactory.decodeResource(getResources(), R.drawable.btn_double);
		bButtonDouble_H = bBtnDouble.getHeight();
		bButtonDouble_W = bBtnDouble.getWidth();
	
		
		back_card = BitmapFactory.decodeResource(getResources(), R.drawable.vp_cards_back);
		eState = State.eStateBegin;
		
		bgr = BitmapFactory.decodeResource(getResources(), R.drawable.bjgreen);
	}

	@SuppressLint({ "ResourceAsColor", "DrawAllocation" })
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Style.STROKE);
		
		canvas.drawBitmap(bgr, 0, 0, null);

		for (int i = 0; i < v_PlayerCard.size(); i++){
			canvas.drawBitmap(v_PlayerCard.elementAt(i), (width/5)+50*i, height-bCard_H*2, null);
		}
		
		for (int i = 0; i < v_CropieCard.size(); i++){
			canvas.drawBitmap(v_CropieCard.elementAt(i), (width/5)+50*i, bCard_H/2, null);
		}

		if (eState == State.eStateBegin){
			canvas.drawBitmap(bBtnDeal, (width/2)-(bButtonDeal_W/2), height-bButtonDeal_H, null);
		}
		
		if (eState == State.eStateDeal){
			canvas.drawBitmap(bBtnHit, (width/2)-(bButtonHit_W/2), height-bButtonHit_H, null);
			//canvas.drawBitmap(bBtnDouble, (width/3)-(bButtonStand_W/2)+bButtonHit_W+bButtonDouble_W, height-bButtonStand_H, null);
			canvas.drawBitmap(bBtnStand, (width/2)-(bButtonStand_W/2)+bButtonHit_W, height-bButtonStand_H, null);
		}
		
		
		if (!bDealTrue){
			paint.setTextSize(25);
			String PoinsCards = String.valueOf(Points);
			canvas.drawText(PoinsCards, width/6, (height-bCard_H*2), paint);

			String sPointsCrupie = String.valueOf(PointsCrupie);
			canvas.drawText(sPointsCrupie, (width/6), (bCard_H/2), paint);

		}

		gCoint.DrawCoints(canvas);
		
		if (bNotBet){
			paint.setColor(Color.RED);
			paint.setStyle(Style.STROKE);
			paint.setTextSize(nFontSize);
			canvas.drawText("NO BET", (width/2)-100, (height/2)+50, paint);
		}
		
		if (bDrawLost){
			paint.setColor(Color.RED);
			paint.setStyle(Style.STROKE);
			paint.setTextSize(nFontSize);
			canvas.drawText("YOU LOST", (width/2)-100, (height/2)+50, paint);
		}
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int action = event.getAction();


		float x = event.getX();  // or getRawX();
		float y = event.getY();
		
		if (action == MotionEvent.ACTION_DOWN && eState == State.eStateStand){
			bDrawLost = false;
			bDealTrue = true;
			Points = 0;
			PointsCrupie = 0;
			for (int i = 0; i < v_PlayerPoints.length; i++){
				v_PlayerPoints[i] = 0;
			}
			
			v_PlayerCard.removeAllElements();
			v_CropieCard.removeAllElements();
			gCoint.BetCoint = 0;
			nNextHit = 2;
			
			SetStatusChange(State.eStateBegin);
		}
		
		if (action == MotionEvent.ACTION_DOWN && eState == State.eStateBegin){
			if (gCoint.BetCoint == 0){
				bNotBet = true;
			}
		}
		
		if (!bNotBet){

			//Button Hit
			if (action == MotionEvent.ACTION_DOWN && eState == State.eStateDeal){
				if (x >= (width/2)-(bButtonHit_W/2) && x < ((width/2)-(bButtonHit_W/2) + bBtnHit.getWidth())
						&& y >= height-bButtonHit_H && y < (height-bButtonHit_H + bBtnHit.getHeight())) {

					GetHitPlayerCards();
					Up21Scenario();

				}
			}

			//Button Stand
			if (action == MotionEvent.ACTION_DOWN && eState == State.eStateDeal){
			  	if (x >= (width/2)-(bButtonStand_W/2)+bButtonHit_W && x < ((width/2)-(bButtonStand_W/2)+bButtonHit_W + bBtnStand.getWidth())
			  			&& y >= height-bButtonHit_H && y < (height-bButtonHit_H + bBtnHit.getHeight())) {
				    		StandScenario();
			   	}
			}

			//Button Deal
			if (action == MotionEvent.ACTION_DOWN && eState == State.eStateBegin){
				if (x >= (width/2)-(bButtonDeal_W/2) && x < ((width/2)-(bButtonDeal_W/2) + bBtnDeal.getWidth())
						&& y >= height-bButtonDeal_H && y < (height-bButtonDeal_H + bBtnDeal.getHeight())) {
					bDealTrue = false;
					GetPlayerCards();
					SetStatusChange(State.eStateDeal);
				}
			}

		}

		if (action == MotionEvent.ACTION_DOWN){
			if (Points == 0)
				gCoint.Click(x, y);
			if (gCoint.BetCoint != 0)
				bNotBet = false;
		}

		invalidate();
		return true;

	}
	
	private void GetHitPlayerCards() {
		// TODO Auto-generated method stub
		int GeneratorCardsNum = Generator.nextInt(bitmaps.length);
		Points += CardsBet[GeneratorCardsNum];
		v_PlayerPoints[nNextHit] = CardsBet[GeneratorCardsNum];
		v_PlayerCard.add(bitmaps[GeneratorCardsNum]);
		nNextHit++;
	}

	public void GetPlayerCards(){
		for (int i = 0; i < 2; i++){ 
			int GeneratorCardsNum = Generator.nextInt(bitmaps.length);
			Points += CardsBet[GeneratorCardsNum];
			v_PlayerPoints[i] = CardsBet[GeneratorCardsNum];
			v_PlayerCard.add(bitmaps[GeneratorCardsNum]);
			
		}
		
		for (int j = 0; j < 2; j++){ 
			int GeneratorCropieCards = Generator.nextInt(bitmaps.length);
			if (j == 0){
				PointsCrupie += CardsBet[GeneratorCropieCards];
				v_CropiePoints[j] = PointsCrupie;
				v_CropieCard.add(bitmaps[GeneratorCropieCards]);
			}else {
				v_CropieCard.addElement(back_card);
			}
		}
		
	}
	
	public void SetStatusChange(State eStateCange){
		eState = eStateCange;
	}
	
	
	public void Up21Scenario(){
		if (Points > 21){
			Points = 0;
			for (int i =0; i < v_PlayerPoints.length; i++){
				if (v_PlayerPoints[i] == 11){
					
					v_PlayerPoints[i] = 1;
				}
				Points += v_PlayerPoints[i];
			}
			
			if (Points > 21){
				bDrawLost = true;
				
				int GeneratorCropieCards = Generator.nextInt(bitmaps.length);
				PointsCrupie += CardsBet[GeneratorCropieCards];
				v_CropiePoints[1] = PointsCrupie;
				v_CropieCard.set(1,bitmaps[GeneratorCropieCards]);
				
				SetStatusChange(State.eStateStand);
			}
			
		}
	}
	
	public void StandScenario(){

		
		if (PointsCrupie < 17){
			int GeneratorCropieCards = Generator.nextInt(bitmaps.length);
			PointsCrupie += CardsBet[GeneratorCropieCards];
			v_CropiePoints[1] = PointsCrupie;
			v_CropieCard.set(1,bitmaps[GeneratorCropieCards]);	
		}
		
		for (int j = 0; j < v_CropiePoints.length; j++){

			
			if (v_CropiePoints[j] == 11){

				v_CropiePoints[j] = 1;
			}
			//PointsCrupie += v_CropiePoints[j];
		}
	}
		
	
}
